const express =require('express')
const fs =require('fs')
const app = express();
const {createBundleRenderer} = require('vue-server-renderer')
const serverBundle=require('../dist/server/vue-ssr-server-bundle.json')
const clientManifest=require('../dist/client/vue-ssr-client-manifest.json')
const renderer =createBundleRenderer(serverBundle,{
	runInNewContext:false,
	template:fs.readFileSync('../public/index.temp.html','utf-8'),
	clientManifest
})

app.use(express.static('../dist/client',{index:false}))

app.get('*', async (req,res)=>{
	
	try{
		const content={
			url:req.url,
			title:"SSR学习"
		}
		const html= await renderer.renderToString(content)
		console.log(html)
		res.send(html)
	}catch{ 
		res.status(500).send("错误")
	}
	
	
	
})
app.listen(3000,()=>{
	console.log("服务启动成功!! http://127.0.0.1:3000/")
	
})
